package com.zlz.parent.basic.springboot.starter.trace;

/**
 * @author zhulinzhong
 * @date 2022-03-07 20:07:40
 */
public class TraceContext {

    public static final ThreadLocal<LoginContext> TRACE = new ThreadLocal<>();

    private TraceContext() {
    }

    /**
     * 请求开始获取并保存用户信息
     *
     * @param loginContext
     */
    public static void init(LoginContext loginContext) {
        TRACE.set(loginContext);
    }

    /**
     * 用户id
     *
     * @return
     */
    public static LoginContext getUserInfo() {
        LoginContext loginContext = TRACE.get();
        if (null == loginContext) {
            return LoginContext.builder().build();
        }
        loginContext.setUserId("10086");
        return loginContext;
    }

    /**
     * 请求完成销毁用户信息
     */
    public static void destroy() {
        TRACE.remove();
    }

}
